package com.lottery.ruok.lottrey.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.ChargeArtificialActivity;
import com.lottery.ruok.lottrey.activity.ChargeBankActivity;
import com.lottery.ruok.lottrey.activity.ChargeThreeActivity;
import com.lottery.ruok.lottrey.activity.ShanYinRechargeActivity;
import com.lottery.ruok.lottrey.adapter.ChargeItemsAdapter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.PayPostRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetPayTypeResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.PayPostResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import com.lottery.ruok.lottrey.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import utils.Utils;

public class ChargeItemsFragment extends BaseFragment {


    private View rootView;
    @BindView(R.id.view_listView)
    ListView view_listView;
    private ChargeItemsAdapter adapter;
    private String money = "";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        initData();
    }

    GetPayTypeResponse data;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_charge_items, null);
        ButterKnife.bind(this, rootView);

        initView();
        return rootView;

    }

    /**
     * 初始化数据
     */
    private void initData() {

    }

    /**
     * 初始化View
     */
    private void initView() {
        if (adapter == null) {
            adapter = new ChargeItemsAdapter(getActivity());
        }
        if (data != null) {
            adapter.setList(data.getList(), data.getTitle());
        }
        view_listView.setAdapter(adapter);
        view_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (money.length() == 0 || Utils.parseFloat(money) == 0) {
                    Utils.showToast("请输入正确的金额");
                    return;
                }
                GetPayTypeResponse.GetPayItemsResponse itemData = (GetPayTypeResponse.GetPayItemsResponse) adapterView.getItemAtPosition(i);
                if (itemData == null) {
                    return;
                }
                if (Utils.parseFloat(money) < itemData.getMob_min()) {
                    Utils.showToast("输入金额过小,最小金额为" + itemData.getMob_min());
                    return;
                }
                if (Utils.parseFloat(money) > itemData.getMob_max()) {
                    Utils.showToast("输入金额过大,最大金额为" + itemData.getMob_max());
                    return;
                }

               /* switch (data.getTitle()) {
                    case "人工通道":
                        if ("transfer".equals(itemData.getName_alias())) {
                            ChargeBankActivity.startActivity(getActivity(), itemData, money);
                        } else {
                         ChargeArtificialActivity.startActivity(getActivity(), itemData, money);
//                            ShanYinRechargeActivity.gotoShanYinRecharge(getActivity());
                        //    checkShanYinRecharge(itemData);
                        }
                        break;
                    case "一键支付":
                       *//* if (itemData.getId().equals("44")){
                            checkShanYinRecharge(itemData);
                        }else {*//*
                            checkStart(itemData);


                        break;
                    case "快捷扫码":
                        if (itemData.getRedirects().equals("0")){
                            checkShanYinRecharge(itemData);
                        }else {
                            checkStart(itemData);
                        }
                        break;
                }*/

                if (itemData.getPayType().equals("MT")){
                    //人工通道改为公司入款
                    if ("transfer".equals(itemData.getName_alias())) {
                        ChargeBankActivity.startActivity(getActivity(), itemData, money);
                    } else {
                        ChargeArtificialActivity.startActivity(getActivity(), itemData, money);
//                            ShanYinRechargeActivity.gotoShanYinRecharge(getActivity());
                        //    checkShanYinRecharge(itemData);
                    }
                }else {
                    if (StringUtils.isNotEmpty(itemData.getRedirects())) {
                        if (itemData.getRedirects().equals("0")) {
                            //联赢支付
                            checkShanYinRecharge(itemData);
                        } else {
                            //扫码支付
                            checkStart(itemData);
                        }
                    }else {
                        //扫码支付
                        checkStart(itemData);
                    }

                }
            }
        });
    }

    private void checkShanYinRecharge(final GetPayTypeResponse.GetPayItemsResponse itemData) {
        RetrofitBase.getRetrofit().create(PayPostRequest.class).getResult(UserInfo.getToken(),
                itemData.getId(),
                itemData.getPayType(),
                money)
                .compose(RxHelper.<PayPostResponse>handleResult())
                .subscribe(new RxSubscribe<PayPostResponse>(getActivity()) {
                    @Override
                    protected void _onNext(final PayPostResponse payPostResponse) {

                        ShanYinRechargeActivity.gotoShanYinRecharge(getActivity(),payPostResponse,itemData);
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }


    private void checkStart(final GetPayTypeResponse.GetPayItemsResponse itemData) {
        RetrofitBase.getRetrofit().create(PayPostRequest.class).getResult(UserInfo.getToken(),
                itemData.getId(),
                itemData.getPayType(),
                money)
                .compose(RxHelper.<PayPostResponse>handleResult())
                .subscribe(new RxSubscribe<PayPostResponse>(getActivity()) {
                    @Override
                    protected void _onNext(final PayPostResponse payPostResponse) {

                        if (payPostResponse.getRedirect() == 0) {
                            //二维码
                            ChargeThreeActivity.startActivity(getActivity(), payPostResponse, money, itemData.getItem_name());

                        } else {
                            //网页支付
                            Utils.showToast("在打开的网站中完成支付");
                            if (!Utils.isEmpty(payPostResponse.getCode_img_url())) {
                                Utils.openBrowser(payPostResponse.getCode_img_url(), getActivity());
                            }
                        }
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }

    public void setMoney(String money) {
        this.money = money;
    }

    public void setData(GetPayTypeResponse data) {
        this.data = data;
        if (adapter != null) {
            adapter.setList(data.getList(), data.getTitle());
        }
    }
}
